import { asyncRouter, constantRoutes } from '@/router'
// vuex中的permission模块存静态路由+当前用户的权限路由
const state = {
  routes: constantRoutes // 所有人默认拥有静态路由
}
const mutations = {
  // newRoutes 可以认为是用户登录 通过权限获取动态路由
  setRoutes(state, newRoutes) {
    // 用户登录，根据权限在静态路由上添加动态路由
    state.routes = [...constantRoutes, ...newRoutes]
  }
}
const actions = {
  // 筛选权限路由
  // 第二个参数为当前用户所拥有的菜单权限
  filterRoutes(context, menus) {
    const routes = []
    //  筛选出动态路由中能和menus对应的路由
    menus.forEach(key => {
      routes.push(...asyncRouter.filter(item => item.name === key))  
    })
    context.commit('setRoutes', routes)
    return routes    
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
